﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JiraGenCommon.JiraServices;
using JiraGenCommon.Model;
using JiraGenerator.Properties;

namespace JiraGenerator
{
    public sealed class UIUtils
    {
        private static string messageCaption = Resources.Messagebox_Caption;

        public static DialogResult ShowMessageBox(string messageText)
        {
            return MessageBox.Show(messageText, messageCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static DialogResult ShowMessageBox(string messageText, MessageBoxButtons msgButton, MessageBoxIcon msgIcon)
        {
            return MessageBox.Show(messageText, messageCaption, msgButton, msgIcon);
        }

        public static bool IsNewExtension(string fileName)
        {
            return fileName.EndsWith(".xslx");
        }

        public static int CountJiraTask(List<JiraGenContract> listJira)
        {
            return listJira.Sum(t => t.Tasks.Count*3);
        }

        public static Dictionary<string, bool> GetTreeNodeCheckedStatus(TreeView treeView, out int checkedCount)
        {
            checkedCount = 0;

            var results = new Dictionary<string, bool>();
            foreach (TreeNode node in treeView.Nodes)
            {
                if (node.Checked)
                {
                    results.Add(node.Name, node.Checked);
                    checkedCount += 1;
                }

                checkedCount += GetAllChildNodesValue(node, results);
            }

            return results;
        }

        // Updates all child tree nodes recursively.
        private static int GetAllChildNodesValue(TreeNode treeNode, Dictionary<string, bool> results)
        {
            int count = 0;
            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Checked)
                {
                    results.Add(node.Name, node.Checked);
                    count += 1;
                }

                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    count += GetAllChildNodesValue(node, results);
                }
            }
            return count;
        }

        public static List<string> GetListSheetName(DataSet dataSet)
        {
            if (dataSet == null) throw new ArgumentNullException("dataSet");

            return (from DataTable table in dataSet.Tables select table.TableName).ToList();
        }

        public static void BindVersions(ComboBox cbo, string projectKey)
        {
            var versions = JiraService.Instance.GetVersions(projectKey);
            if(versions != null)
            {
                cbo.DataSource = versions;
                cbo.DisplayMember = "name";
                cbo.ValueMember = "id";
                cbo.Refresh();
            }
        }

        public static string ConvertToSheetname(string sheetName)
        {
            var excelSheetName = string.Format("{0}$", sheetName);

            if(sheetName.IndexOf(" ") != -1)
            {
                excelSheetName = string.Format("'{0}$'", sheetName);
            }

            return excelSheetName;
        }
    }
}
